package game.mechanics;

import game.enums.EquipSlots;
import game.referenceValues.Feat;
import game.units.Attribute;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Item {
	
	// static time!
	private static Map<Integer, Item> items = new HashMap<Integer, Item>();

	private Map<String, String> attributes = new HashMap<String, String>();
	private int id;

	public Item()
	{
		
	}
	
	public Item (String name, String description, int quantity) {
		this.name = name;
		this.description = description;
		this.quantity = quantity;
		
		//Trocar futuramente para pegar do xml
		this.weight = 0;
	}
	
	private String name;
	private int quantity;
	private String description;
	private int weight;
	
	//gerar um construtor do item a partir do id
	private int itemIdentifier;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	
	public ArrayList<Item> getItemList()
	{
		ArrayList<Item> itens = new ArrayList<Item>();
		itens.add(new Item ("Gold", "Wolrd Currency", 0));
		itens.add(new Item ("Longsword", "Standard Long Sword", 0));
		itens.add(new Item ("Potion of Cure Light Wounds", "Cures 1d8 life points", 0));
		return itens;
	}
	
	public String getAttribute(String attr) {
		// TODO Auto-generated method stub
		return attributes.get(attr);
	}

	public int getItemIdentifier() {
		return itemIdentifier;
	}

	public void setItemIdentifier(int itemIdentifier) {
		this.itemIdentifier = itemIdentifier;
	}	
		
	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public static Item getItemByID(int itemId) {
//		System.out.println("item id: " + itemId + " " + items.get(itemId));
		return items.get(itemId);
	}

	public int getId() {
		return getItemIdentifier();
	}
	
	public static void loadDNDItems()
	{
		items = new HashMap<Integer, Item>();
		try {
			FileInputStream XMLin = new FileInputStream("items.xml");
			SAXBuilder sb = new SAXBuilder();
			try {
				Document doc = sb.build(XMLin);
				// Recuperamos o elemento root
				Element xmlItens = doc.getRootElement();
				// Recuperamos os elementos filhos (children)
				List elements = xmlItens.getChildren();
				Iterator i = elements.iterator();
				while (i.hasNext()) {
					Weapon weapon = new Weapon();
					int type = 0;
					
					Element element = (Element) i.next();
					weapon.setItemIdentifier(Integer.parseInt((element.getChildText("ID"))));
					weapon.setDescription(element.getChildText("Description"));
					weapon.setName(element.getChildText("Name"));
					weapon.setQuantity(Integer.parseInt((element.getChildText("Quantity"))));
										
					Element el = element.getChild("Equip");
					List equipElements = el.getChildren();
					Iterator j = equipElements.iterator();
					if (j.hasNext()) {
						type++;
						//o item eh um equip
						
						weapon.setSlot(EquipSlots.valueOf(el.getChildText("Slot")));
						ArrayList<Attribute> bonuses = new ArrayList<Attribute>();
						Element el3 = el.getChild("AttributeBonus");
						List bonus = el3.getChildren();
						Iterator l = bonus.iterator();
						if (l.hasNext()) {					
						}
						weapon.setBonuses(bonuses);
						weapon.setWeight(Integer.parseInt((el.getChildText("Weight"))));
						
						ArrayList<Feat> requirements = new ArrayList<Feat>();
						Element el4 = el.getChild("Requirements");
						List req = el4.getChildren();
						Iterator m = bonus.iterator();
						if (m.hasNext()) {
							requirements.add(new Feat(el4.getChildText("Feat")));
						}						
						weapon.setRequirements(requirements);
						weapon.setArmorClass(Integer.parseInt((el.getChildText("ArmorClass"))));
						weapon.setMaxDexBonus(Integer.parseInt((el.getChildText("MaxDexBonus"))));
						
						Element el2 = el.getChild("Weapon");
						List weaponElements = el2.getChildren();
						Iterator k = weaponElements.iterator();
						if (k.hasNext()) {
							type++;
							weapon.criticalMult = (Integer.parseInt((el2.getChildText("CriticalMult"))));
							weapon.minCritical = (Integer.parseInt((el2.getChildText("MinCritical"))));
							weapon.range = (Integer.parseInt((el2.getChildText("Range"))));
							String[] dmg  = el2.getChildText("DmgBonus").split(",");
							int[] intDmg;
							if(dmg.length > 0)
							{
								intDmg = new int[dmg.length];							
								for(int index = 0; index <  dmg.length; index++)
								{
									intDmg[index] = Integer.parseInt(dmg[index]);
								}
							}
							else
							{
								intDmg = new int[1];
								intDmg[0] = 0;
							}
							weapon.setDamage(new Damage(intDmg, el2.getChildText("Die")));
							int hands = (Integer.parseInt((el2.getChildText("Hands"))));
							if(hands == 1)
							{
								weapon.setSlot(EquipSlots.OffHand);
							}
							weapon.setHands(hands);
						}
					}
					switch (type) {
					case 0:
						Item item = new Item(weapon.getName(), weapon.getDescription(), weapon.getQuantity());
						item.itemIdentifier = weapon.getItemIdentifier();
						items.put(item.getItemIdentifier(), item);
						break;
					case 1:
						Equip equip = new Equip(weapon.getName(), weapon.getDescription(), weapon.getQuantity(), weapon.getSlots(), weapon.getBonuses(), weapon.getWeight(), weapon.getRequirements(), weapon.getArmorClass(), weapon.getMaxDexBonus());						
						equip.setItemIdentifier(weapon.getItemIdentifier());
						items.put(equip.getItemIdentifier(), equip);
						break;
					case 2:
						items.put(weapon.getItemIdentifier(), weapon);
						break;
					default:
						break;
					}
					
				}
				XMLin.close();
			} catch (JDOMException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Map<Integer, Item> getItems() {
		return items;
	}
	
}
